﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MisClient
{
    public partial class FrmReturnStatistics : BaseForm
    {
        private IBLL.IDepart departBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteDepart();
        private IBLL.IRacf racfBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteRacf();
        private IBLL.IGoods goodsBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteGoods();
        private IBLL.IType typeTjBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteType();
        private IBLL.ICustomer cusBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteCustomer();
        private IBLL.ISupply supplyBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSupply();

        private WinPrint winPrint = new WinPrint();

        private int dataGridView = 0;

        public FrmReturnStatistics()
        {
            InitializeComponent();
        }

        private void FrmReturnStatistics_Load(object sender, EventArgs e)
        {
            this.openStarDate.Value = DateTime.Now.Date.AddDays(-7);
            this.txtDepCode.Text = accountUser.DeptCode;
            this.openStarDate.Focus();
        }

        private void openStarDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.openEndDate.Focus();
            }
        }

        private void openEndDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtID.Focus();
            }
        }

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtID.Text.Trim()))
                {
                    MisEntity.Goods goods = goodsBLL.EndsWithID(this.txtID.Text.Trim());
                    if (goods == null)
                    {
                        MessageBox.Show("无此商品,请先设置！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtID.Focus();
                        return;
                    }
                    this.txtGoods.Text = goods.GOODS;
                    this.txtDM.Text = goods.DM;
                    this.txtID.Text = goods.ID;
                }
                this.txtOper.Focus();
            }
        }

        private void txtOper_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtNo.Focus();
            }
        }

        private void txtNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtDepCode.Focus();
            }
        }

        private void txtDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
                {
                    MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
                if (depart == null)
                {
                    MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                this.txtBoxDep.Text = depart.DepName;
                this.txtGysCode.Focus();
            }
        }

        private void txtGysCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtGysCode.Text.Trim()))
                {
                    MisModel.Supply supply = supplyBLL.GetById(this.txtGysCode.Text.Trim());
                    if (supply == null)
                    {
                        MessageBox.Show("供应商不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtGysCode.Focus();
                        return;
                    }
                    else
                        this.txtGysName.Text = supply.SuppName;
                }
                this.txtHt.Focus();
            }
        }

        private void txtHt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.comboBox1.Focus();
            }
        }

        private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtBj.Focus();
            }
        }

        private void txtBj_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.textPc.Focus();
            }
        }

        private void textPc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtType1.Focus();
            }
        }

        private void txtType1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtType1.Text.Trim()))
                {
                    MisModel.Type type = typeTjBLL.GetType1ById(this.txtType1.Text.Trim());
                    if (type == null)
                    {
                        MessageBox.Show("统计类型不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtType1.Focus();
                        return;
                    }
                    else
                        this.textBox29.Text = type.TypeName;
                }
                this.txtType2.Focus();
            }
        }

        private void txtType2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtType2.Text.Trim()))
                {
                    MisModel.Type type = typeTjBLL.GetType1ById(this.txtType2.Text.Trim());
                    if (type == null)
                    {
                        MessageBox.Show("统计类型不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtType2.Focus();
                        return;
                    }
                    else
                        this.textBox31.Text = type.TypeName;
                }
                this.txtType3.Focus();
            }
        }

        private void txtType3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtType3.Text.Trim()))
                {
                    MisModel.Type type = typeTjBLL.GetType1ById(this.txtType3.Text.Trim());
                    if (type == null)
                    {
                        MessageBox.Show("统计类型不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtType3.Focus();
                        return;
                    }
                    else
                        this.textBox9.Text = type.TypeName;
                }
                this.btnOk.Focus();
            }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            Point mousePoint = MousePosition;
            Point point = this.PointToClient(mousePoint);
            this.contextMenuStrip1.Show(this, point);
        }

        private void toolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
            {
                MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
            if (depart == null)
            {
                MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            StringBuilder sb = new StringBuilder();
            this.dataGridView1.Rows.Clear();
            sb.Append(" 1=1 ");
            if (!this.txtDepCode.Text.Trim().Equals("1"))
            {
                sb.Append(" m.dep_id=@depId");
                dictionary.Add("@depId", this.txtDepCode.Text.Trim());
            }
            sb.Append(" and m.date>=@startDate and m.date<=@endDate");
            dictionary.Add("@startDate", this.openStarDate.Value.ToShortDateString());
            DateTime time = DateTime.Parse(this.openEndDate.Value.ToShortDateString());
            time = time.Date.AddDays(1);
            dictionary.Add("@endDate", time.ToShortDateString());
            if (!string.IsNullOrEmpty(this.txtID.Text.Trim()))
            {
                sb.Append(" and m.id=@id");
                dictionary.Add("@id", this.txtID.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtGysCode.Text.Trim()))
            {
                sb.Append(" and g.supp_id=@suppId");
                dictionary.Add("@suppId", this.txtGysCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtNo.Text.Trim()))
            {
                sb.Append(" and m.CON_NO=@no");
                dictionary.Add("@no", this.txtNo.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtOper.Text.Trim()))
            {
                sb.Append(" and m.OPER_ID=@oper");
                dictionary.Add("@oper", this.txtOper.Text.Trim());
            }
            string tjType = this.comboBox1.SelectedItem.ToString();
            DataTable dt = racfBLL.GetStatisticsAll(sb.ToString(), dictionary, tjType);
            decimal slp = 0;
            decimal price1 = 0;
            decimal price2 = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                if ("按商场".Equals(tjType))
                {
                    this.dataGridView1.Columns[0].HeaderCell.Value = "商场";
                    this.dataGridView1.Columns[1].HeaderCell.Value = "商场名称";
                }
                else if ("按部门".Equals(tjType))
                {
                    this.dataGridView1.Columns[0].HeaderCell.Value = "部门";
                    this.dataGridView1.Columns[1].HeaderCell.Value = "部门名称";
                }
                else if ("按合同号".Equals(tjType))
                {
                    this.dataGridView1.Columns[0].HeaderCell.Value = "合同号";
                    this.dataGridView1.Columns[1].HeaderCell.Value = "柜组名称";
                }
                else if ("按供应商".Equals(tjType))
                {
                    this.dataGridView1.Columns[0].HeaderCell.Value = "供应商";
                    this.dataGridView1.Columns[1].HeaderCell.Value = "供应商名称";
                }
                else if ("按柜组".Equals(tjType))
                {
                    this.dataGridView1.Columns[0].HeaderCell.Value = "柜组";
                    this.dataGridView1.Columns[1].HeaderCell.Value = "柜组名称";
                }
                else if ("按商品".Equals(tjType))
                {
                    this.dataGridView1.Columns[0].HeaderCell.Value = "商品编号";
                    this.dataGridView1.Columns[1].HeaderCell.Value = "商品名称";
                }
                else
                {
                    this.dataGridView1.Columns[0].HeaderCell.Value = "柜组";
                    this.dataGridView1.Columns[1].HeaderCell.Value = "柜组名称";
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    String[] DataRow = new String[5];
                    DataRow[0] = dt.Rows[i][0].ToString();
                    DataRow[1] = dt.Rows[i][1].ToString();
                    DataRow[2] = dt.Rows[i][2].ToString();
                    DataRow[3] = string.Format("{0}", decimal.Parse(dt.Rows[i][3].ToString()).ToString("f4"));
                    DataRow[4] = dt.Rows[i][4].ToString();
                    slp += decimal.Parse(DataRow[2]);
                    price1 += decimal.Parse(DataRow[3]);
                    price2 += decimal.Parse(DataRow[4]); 
                    this.dataGridView1.Rows.Add(DataRow);
                }
                this.label17.Text = string.Format("● 共{3}条 合计数量：{0} 售价金额：{1} 进货金额：{2}", (int)slp, price1.ToString("f4"), price2, dt.Rows.Count);
            }
            else
            {
                this.label17.Text = "";
                MessageBox.Show("查无符合条件的记录！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            string _Title = "新江厦汽车销售公司商品退货统计表";
            Font _TitleFont = new Font("宋体", 20, FontStyle.Bold);
            Point _CurrentPoint = new Point(30, 30);
            _CurrentPoint.X = (e.PageBounds.Width) / 2 - (int)(e.Graphics.MeasureString(_Title, _TitleFont).Width) / 2;
            e.Graphics.DrawString(_Title, _TitleFont, new SolidBrush(Color.Black), _CurrentPoint);
            StringBuilder sb = new StringBuilder();
            decimal sp = 0;
            decimal price1 = 0;
            decimal price2 = 0;
            sb.Append(string.Format("部门：{0}\n", this.txtDepCode.Text.Trim()));
            sb.Append(string.Format("统计日期：{0}至{1}\n", this.openStarDate.Value.ToShortDateString(), this.openEndDate.Value.ToShortDateString()));
            sb.Append(string.Format("统计方式：{0}\n", this.comboBox1.SelectedItem.ToString()));
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(string.Format("{0}       {1}                       发生数量     销价金额      进价金额\n", this.dataGridView1.Columns[0].HeaderCell.Value, this.dataGridView1.Columns[1].HeaderCell.Value));
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            if (this.dataGridView1.Rows.Count > 0)
            {
                DataGridViewRow dr = null;
                for (int i = dataGridView; i < this.dataGridView1.RowCount; i++, dataGridView++)
                {
                    dr = this.dataGridView1.Rows[i];
                    sb.Append(winPrint.getLenStr(14, dr.Cells[0].Value.ToString(), 1));
                    sb.Append(winPrint.getGoodsStr(26, dr.Cells[1].Value.ToString()));
                    sb.Append(winPrint.getLenStr(12, decimal.Parse(dr.Cells[2].Value.ToString()).ToString("f2")));
                    sp += decimal.Parse(dr.Cells[2].Value.ToString());
                    sb.Append(winPrint.getLenStr(13, decimal.Parse(dr.Cells[3].Value.ToString()).ToString("f2")));
                    price1 += decimal.Parse(dr.Cells[3].Value.ToString());
                    sb.Append(winPrint.getLenStr(13, decimal.Parse(dr.Cells[4].Value.ToString()).ToString("f2"))).Append("\n");
                    price2 += decimal.Parse(dr.Cells[4].Value.ToString());
                    if (i != 0 && i % 42 == 0)
                    {
                        dataGridView++;
                        break;
                    }
                }
            }
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(string.Format("合计数量：{0}  售价金额：{1}  进价金额：{2}", sp.ToString("f2"), price1.ToString("f2"), price2.ToString("f2"))).Append("\n");
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(string.Format("制表：{0} 姓名{1}      日期：{2}", accountUser.UserCode, accountUser.Oper, DateTime.Now));

            _CurrentPoint.X = 30;
            _CurrentPoint.Y += (int)(e.Graphics.MeasureString(_Title, _TitleFont).Height) + 20;
            Font _ContentFont = new Font("宋体", 12);
            e.Graphics.DrawString(sb.ToString(), _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            if (dataGridView < this.dataGridView1.RowCount - 1)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
        }

        private void btnPri1_Click(object sender, EventArgs e)
        {
            this.dataGridView = 0;
            this.printPreviewDialog1.Document = this.printDocument1;
            this.printPreviewDialog1.PrintPreviewControl.Zoom = 1.0;
            this.printPreviewDialog1.ShowDialog();
        }
    }
}
